package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.CommentBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: CommentAdapter
 * @Description: 评论页面adapter
 * @Author: ljp
 * @CreateDate: 2018/11/1 16:16
 * @Version: 1.0
 */
public class CommentAdapter extends BaseAdapter<CommentBean.PageBean.ListBean> {
    private CommentBean.PageBean.ListBean listBean;
    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(context,bitmap,view);
                /*GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(view);*/
            }
        }
    };
    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public CommentAdapter(Context context, List<CommentBean.PageBean.ListBean> tlist, int mLayout,CommentBean.PageBean.ListBean listBean) {
        super(context, tlist, mLayout);
        this.listBean = listBean;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeadViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof HeadViewHolder){
            Log.i("onBindViewHolder", "onBindViewHolder: ---------"+listBean.toString());
            HeadViewHolder headViewHolder = (HeadViewHolder) viewHolder;
            setImage(listBean.getAvatarUrl(), headViewHolder.imageIcon);
            headViewHolder.tvNikeName.setText(listBean.getNickName());
            headViewHolder.tvTitle.setText(listBean.getContent());
            headViewHolder.tvLikeNumber.setText(listBean.getFabulousCount()+"");
            if (listBean.getuType() == 2 ) {//判断是否是主播
                headViewHolder.imageIsanchor.setVisibility(View.VISIBLE);
            } else {
                headViewHolder.imageIsanchor.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(listBean.getCreateDate())) {
                String standardDate = Utils.getStandardDate(listBean.getCreateDate());//将返回时间转换为多久前
                headViewHolder.tvDate.setText(standardDate);
            }
            if (!TextUtils.isEmpty(listBean.getvType()) &&
                    listBean.getvType().equals("1")) {//判断是vip等级 1 月 2 年 0 不是会员
                headViewHolder.imageVip.setVisibility(View.VISIBLE);
                headViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
            } else if (listBean.getvType().equals("2")) {
                headViewHolder.imageVip.setVisibility(View.VISIBLE);
                headViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
            } else {
                headViewHolder. imageVip.setVisibility(View.GONE);
            }
            if (listBean.getGender() == 1) {//判断性别  1男 2女
                headViewHolder.imageSex.setImageResource(R.mipmap.boy);
                headViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            } else if (listBean.getGender() == 2) {
                headViewHolder.imageSex.setImageResource(R.mipmap.girl);
                headViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            if (!TextUtils.isEmpty(listBean.getBirthday())) {
                String substring = listBean.getBirthday().substring(0, 4);
                String time = Utils.getStringTime(new Date());
                int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                headViewHolder.tvAge.setText(age + "");
            }
            headViewHolder.tvCommNumber.setText(listBean.getDiscussCount()+"");
            headViewHolder.linearLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClick.setLikeClick(v,listBean);
                }
            });

        }else if(viewHolder instanceof ItemViewHolder){
            if(position == 0){
                return;
            }
            position = position - 1;
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            CommentBean.PageBean.ListBean commentBean = tlist.get(position);
            if (!TextUtils.isEmpty(commentBean.getvType()) &&
                    commentBean.getvType().equals("1")) {//判断是vip等级 1 月 2 年 0 不是会员
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
            } else if (commentBean.getvType().equals("2")) {
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
            } else {
                itemViewHolder.imageVip.setVisibility(View.GONE);
            }
            if ( commentBean.getuType()== 2) {//判断是否是主播
                itemViewHolder.imageIsanchor.setVisibility(View.VISIBLE);
            } else {
                itemViewHolder.imageIsanchor.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(commentBean.getBirthday())) {
                String substring = commentBean.getBirthday().substring(0, 4);
                String time = Utils.getStringTime(new Date());
                int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                itemViewHolder.tvAge.setText(age + "");
            }
            if (commentBean.getGender() == 1){//判断性别  1男 2女
                itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            }else if(commentBean.getGender() == 2){
                itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            itemViewHolder.tvConent.setText(commentBean.getContent());
            if(!TextUtils.isEmpty(commentBean.getCreateDate())) {
                String standardDate = Utils.getStandardDate(commentBean.getCreateDate());//将返回时间转换为多久前
                itemViewHolder.tvDate.setText(standardDate);
            }
            setImage(commentBean.getAvatarUrl(),itemViewHolder.circleIcon);
            itemViewHolder.tvNikeName.setText(commentBean.getNickName());
        }

    }
    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = view;
                        Bundle bundle = new Bundle();
                        //bundle.putString("tag",url);
                        bundle.putByteArray("bitmap", bitmap);
                        message.setData(bundle);
                        handler.sendMessage(message);
                        //  }
                    }
                    @Override
                    public void onDownFileProgress(String progress) {

                    }
                });
            }
        }).start();

    }

    class HeadViewHolder extends BaseViewHolder{
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.image_isanchor)
        ImageView imageIsanchor;
        @BindView(R.id.tv_nikename)
        TextView tvNikeName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_comm_number)
        TextView tvCommNumber;
        @BindView(R.id.image_like)
        ImageView imageLike;
        @BindView(R.id.tv_like_number)
        TextView tvLikeNumber;
        @BindView(R.id.linear_like)
        LinearLayout linearLike;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    class ItemViewHolder extends BaseViewHolder{
        @BindView(R.id.circle_icon)
        CircleImageView circleIcon;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_conent)
        TextView tvConent;
        @BindView(R.id.image_isanchor)
        ImageView imageIsanchor;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public interface ItemClick{
        //void setLinearComment(CommentBean.PageBean.ListBean listBean);
        void setLikeClick(View view, CommentBean.PageBean.ListBean listBean);
    }
}
